package com.three.server;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import com.three.bean.UnixTime;

public class ServerHandler extends ChannelInboundHandlerAdapter{

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		ctx.write(msg); // (1)
        ctx.flush(); // (2)
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		// TODO Auto-generated method stub
		cause.printStackTrace();
        ctx.close();
	}
	@Override
    public void channelActive(final ChannelHandlerContext ctx) { // (1)
		ChannelFuture f = ctx.writeAndFlush(new UnixTime());
	    f.addListener(ChannelFutureListener.CLOSE);
    }
	
}
